package com.zhihuichongcheng.application.ui.cricle.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.smartlib.cmnObject.dialog.DialogOnClick;
import com.smartlib.cmnObject.util.DateUtil;
import com.smartlib.cmnObject.util.DeviceUtil;
import com.smartlib.cmnObject.util.DialogUtil;
import com.smartlib.cmnObject.util.ToastOpt;
import com.zhihuichongcheng.application.R;
import com.zhihuichongcheng.application.bean.cricle.CricleCategoryBean;
import com.zhihuichongcheng.application.bean.cricle.CricleImageInfo;
import com.zhihuichongcheng.application.bean.cricle.HealthBean;
import com.zhihuichongcheng.application.bean.mine.UserBean;
import com.zhihuichongcheng.application.constant.NetworkAPI;
import com.zhihuichongcheng.application.glide.GlideCircleTransform;
import com.zhihuichongcheng.application.net.HttpCallback;
import com.zhihuichongcheng.application.net.HttpManager;
import com.zhihuichongcheng.application.ui.BaseApplication;
import com.zhihuichongcheng.application.ui.cricle.activity.CricleDetailActivity;
import com.zhihuichongcheng.application.ui.cricle.activity.ShowImageActivity;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * Created by 3water on 2016/10/28.
 */

public class CricleHealthyAdapter extends RecyclerView.Adapter<CricleHealthyAdapter.ViewHold> {

    private Context mContext;
    private String mUserId;
    private List<HealthBean> mListData = new ArrayList<>();
    private int width;
    private UserBean mUser;

    public CricleHealthyAdapter(Context context) {
        this.mContext = context;
        width = DeviceUtil.getWidth(mContext);
        width = width - DeviceUtil.dip2px(mContext, 65);
        BaseApplication app = (BaseApplication) mContext.getApplicationContext();
        mUser = app.getUser();
    }

    @Override
    public ViewHold onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.listadapter_cricle_healthy, null);
        return new ViewHold(view);
    }

    @Override
    public void onBindViewHolder(ViewHold holder, final int position) {
        final HealthBean bean = mListData.get(position);
        holder.mTvContent.setText(bean.getContent());
        holder.mTvTitle.setText(bean.getTitle());
        CricleCategoryBean categoryBean = bean.getJinbaoHomeCategory();
        final List<CricleImageInfo> list = bean.getImgList();
        if (list != null && list.size() > 0) {
            if (list.size() == 1) {
                holder.mImCricleImg.setVisibility(View.VISIBLE);
                holder.mRcvImg.setVisibility(View.GONE);
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width / 3 * 2, width / 3 * 2);
                params.setMargins(0, 0, 10, DeviceUtil.dip2px(mContext, 5));
                holder.mImCricleImg.setLayoutParams(params);
                Glide.with(mContext).load(list.get(0).getJinbaoHomeImgUrl()).placeholder(R.drawable.ic_cricle_defalut_avatar).into(holder.mImCricleImg);
            } else if (list.size() <= 3) {
                holder.mImCricleImg.setVisibility(View.GONE);
                holder.mRcvImg.setVisibility(View.VISIBLE);
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width, width / 3);
                holder.mRcvImg.setLayoutParams(params);
                CricleDeatilImageAdapter mAdapterImg = new CricleDeatilImageAdapter(mContext, list);
                holder.mRcvImg.setLayoutManager(new GridLayoutManager(mContext, 3));
                holder.mRcvImg.setAdapter(mAdapterImg);
            } else if (list.size() <= 6) {
                holder.mImCricleImg.setVisibility(View.GONE);
                holder.mRcvImg.setVisibility(View.VISIBLE);
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width, width / 3 * 2);
                holder.mRcvImg.setLayoutParams(params);
                CricleDeatilImageAdapter mAdapterImg = new CricleDeatilImageAdapter(mContext, list);
                holder.mRcvImg.setLayoutManager(new GridLayoutManager(mContext, 3));
                holder.mRcvImg.setAdapter(mAdapterImg);
            } else {
                holder.mImCricleImg.setVisibility(View.GONE);
                holder.mRcvImg.setVisibility(View.VISIBLE);
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width, width);
                holder.mRcvImg.setLayoutParams(params);
                CricleDeatilImageAdapter mAdapterImg = new CricleDeatilImageAdapter(mContext, list);
                holder.mRcvImg.setLayoutManager(new GridLayoutManager(mContext, 3));
                holder.mRcvImg.setAdapter(mAdapterImg);
            }
        } else {
            holder.mRcvImg.setVisibility(View.GONE);
            holder.mImCricleImg.setVisibility(View.GONE);
        }

        Date date = DateUtil.parseStringToDate(bean.getCreateTime(), "yyyy年MM月dd日 HH:mm:ss");

        holder.mTvTime.setText(DateUtil.format(date));

        UserBean user = bean.getUser();
        if (user != null) {
            holder.mTvName.setText(user.getUserName());
            Glide.with(mContext).load(user.getLogoUrl()).placeholder(R.drawable.ic_default_avatar).transform(new GlideCircleTransform(mContext)).into(holder.mImAvatar);
            if (categoryBean != null) {
                holder.mTvType.setText(categoryBean.getCategoryName());
            }
            if (!TextUtils.isEmpty(mUserId) && !TextUtils.isEmpty(user.getUserId()) && mUserId.equals(user.getUserId())) {
                holder.mTvDel.setVisibility(View.VISIBLE);
            } else {
                holder.mTvDel.setVisibility(View.GONE);
            }
        } else {
            holder.mTvName.setText("");
            Glide.with(mContext).load("").placeholder(R.drawable.ic_default_avatar).transform(new GlideCircleTransform(mContext)).into(holder.mImAvatar);

        }
        holder.mTvCommentNum.setText(bean.getCommentCount());
        holder.mTvPariseNum.setText(bean.getLikeCount());
        holder.view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, CricleDetailActivity.class);
                intent.putExtra("homeId", bean.getJinbaoHomeId());
                mContext.startActivity(intent);
                ((Activity) mContext).overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
            }
        });
        holder.mTvDel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogUtil.showSimpleAlertDialog(mContext, new DialogOnClick() {
                    @Override
                    public void onClick(Object o) {
                        HttpManager manager = HttpManager.getInstance(mContext);
                        HashMap<String, String> params = new HashMap<String, String>();
                        params.put("token", mUser.getToken());
                        params.put("jinbaoHomeId", bean.getJinbaoHomeId());
                        manager.post(NetworkAPI.DelCricle, new HttpCallback() {
                            @Override
                            public void onSuccess(int code, String value, String msg) {
                                if (code == 1) {
                                    mListData.remove(position);
                                    notifyDataSetChanged();
                                } else {
                                    ToastOpt.createToast(mContext, "删除失败");
                                }
                            }

                            @Override
                            public void onFailure(String value) {
                                ToastOpt.createToast(mContext, "删除失败");
                            }
                        }, params);
                    }
                }, "是否删除这条崇诚家园");

            }
        });
        holder.mImCricleImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, ShowImageActivity.class);
                intent.putParcelableArrayListExtra("list", (ArrayList) list);
                intent.putExtra("index", 0);
                mContext.startActivity(intent);
                ((Activity) mContext).overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mListData.size();
    }

    public void setData(List<HealthBean> list) {
        this.mListData.addAll(list);
    }

    public void clear() {
        mListData.clear();
    }

    public void setUserId(String userId) {
        this.mUserId = userId;
    }

    class ViewHold extends RecyclerView.ViewHolder {
        private ImageView mImAvatar, mImCricleImg;
        private View view;
        private TextView mTvName, mTvTime, mTvTitle, mTvContent, mTvType, mTvCommentNum, mTvPariseNum, mTvDel;
        private RecyclerView mRcvImg;

        public ViewHold(View itemView) {
            super(itemView);
            view = itemView;
            mImAvatar = (ImageView) itemView.findViewById(R.id.im_avatar);
            mTvName = (TextView) itemView.findViewById(R.id.tv_name);
            mTvTime = (TextView) itemView.findViewById(R.id.tv_time);
            mTvTitle = (TextView) itemView.findViewById(R.id.tv_title);
            mTvContent = (TextView) itemView.findViewById(R.id.tv_content);
            mTvType = (TextView) itemView.findViewById(R.id.tv_type);
            mTvCommentNum = (TextView) itemView.findViewById(R.id.tv_comment_num);
            mTvPariseNum = (TextView) itemView.findViewById(R.id.tv_parise_num);
            mImCricleImg = (ImageView) itemView.findViewById(R.id.im_image);
            mRcvImg = (RecyclerView) itemView.findViewById(R.id.rcv_img);
            mTvDel = (TextView) itemView.findViewById(R.id.tv_del);
        }
    }
}
